package com.minglead.springboot.platform.sys.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.minglead.springboot.common.crud.CrudService;
import com.minglead.springboot.common.persistence.Page;
import com.minglead.springboot.common.utils.IdGen;
import com.minglead.springboot.platform.sys.dao.DesktopPlugConfigDao;
import com.minglead.springboot.platform.sys.entity.DesktopPlugConfig;

/**
 * 桌面插件配置Service
 * @author clz
 * @version 2017-09-05
 */
@Service
@Transactional(readOnly = true)
public class DesktopPlugConfigService extends CrudService<DesktopPlugConfigDao, DesktopPlugConfig> {
	
	@Autowired
	private DesktopPlugConfigDao desktopPlugConfigDao;

	public DesktopPlugConfig get(String id) {
		return super.get(id);
	}
	
	public List<DesktopPlugConfig> findList(DesktopPlugConfig desktopPlugConfig) {
		return super.findList(desktopPlugConfig);
	}
	
	public Page<DesktopPlugConfig> findPage(Page<DesktopPlugConfig> page, DesktopPlugConfig desktopPlugConfig) {
		return super.findPage(page, desktopPlugConfig);
	}
	
	@Transactional(readOnly = false)
	public void save(DesktopPlugConfig desktopPlugConfig) {
		super.save(desktopPlugConfig);
	}
	
	@Transactional(readOnly = false)
	public void delete(DesktopPlugConfig desktopPlugConfig) {
		super.delete(desktopPlugConfig);
	}
	
	
	@Transactional
	public List<DesktopPlugConfig> getListByRoleId(String roleId) {
		return desktopPlugConfigDao.getListByRoleId(roleId);
	}
	
	
	@Transactional
	public DesktopPlugConfig getByRoleIdPlugId(String code,String plugId) {
		return desktopPlugConfigDao.getByRoleIdPlugId(code,plugId);
	}
	/**
	 * 重新保存插件配置
	 * @param roleId
	 * @param plugIds
	 */
	@Transactional(readOnly = false)
	public void saveEntity(String roleId, String plugIds) {
		if(StringUtils.isEmpty(roleId))
			return;
		desktopPlugConfigDao.deleteByRoleId(roleId);
		
		if(StringUtils.isEmpty(plugIds))
			return;
		String[] _plugIds = plugIds.split(",");
		int index = 0;
		for(String plugId:_plugIds){
			DesktopPlugConfig config = new DesktopPlugConfig();
			config.setId(IdGen.uuid());
			config.setPlugId(plugId);
			config.setRoleId(roleId);
			config.setSort(index);
			desktopPlugConfigDao.insert(config);
			index++;
		}
	}
}